<?php

class Admin_SitemapController  extends Zend_Controller_Action
{
	private $mapper;
	public function init()
	{
		$this->mapper =  new Admin_Model_TreeNodeMapper(); 
	}
	
	public function refreshFileAction()
	{
		$bootstrap = $this->getInvokeArg('bootstrap');
		$config = $bootstrap->getOptions();
		//$useWWW = isset($config['sitemap']) && isset($config['sitemap']['useWWW'])?$config['sitemap']['useWWW']:false;
		$file = 'sitemap.xml';
		$f = fopen ( $file, 'w' );
		fwrite ( $f, '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n");
		$list = $this->mapper->fetchURLs(true);
		$su = new Zend_View_Helper_ServerUrl();
		$url = $su->getHost();
		/*$p = strpos($url,'www.');
		if (($p!==false) && !$useWWW)
			$url = str_replace('www.','',$url);
		elseif(($p==false) && $useWWW)
			$url = 'www.'.$url;*/
		$url = $su->getScheme().'://'.$url.'/';
		foreach ( $list as $l ) {
			if ($l['Priority']==0) continue;
			$u = ($l['URL']{0}=='/') ? substr($l['URL'],1) : $l['URL'];
			$p = strpos($u,'#',0);
			if ($p!==false)
				$u = substr($u,0,$p);
			$u = $url.$u;
			fwrite ( $f, "<url>\n");
			fwrite ( $f, "  <loc>".$u."</loc>\n");
			fwrite ( $f, "  <lastmod>".$l['LastMod']."</lastmod>\n");
			fwrite ( $f, "  <changefreq>".self::GetFrequency($l['ChangeFreq'])."</changefreq>\n");
			fwrite ( $f, "  <priority>".$l['Priority']."</priority>\n");
			fwrite ( $f, "</url>\n");
		}
		fwrite ( $f, "</urlset>\n");
		fclose ( $f );
		$result = array ('success' => true, 'msg' => 'Файл обновлён' );
		$this->_helper->json ( $result );
	}
	
	public static function GetFrequency($v){
		switch($v)
		{
			case 1:
				return "always";
			case 2:
				return "hourly";
			case 3:
				return "daily";
			case 4:
				return "weekly";
			case 5:
				return "monthly";
			case 6:
				return "yearly";
			case 7:
				return "never";
		}
		return "monthly";
	}
	
	/*public function RefreshUrl() {
		$file = '.htaccess';
		$f = fopen ( $file, 'r+' );
		$contents = fread ( $f, filesize ( $file ) );
		$p1 = strpos ( $contents, '#<list>' );
		$p2 = strpos ( $contents, '#</list>' );
		$result = array ('success' => false, 'msg' => 'Неизвестная ошибка' );
		if ($p1 == false)
			$result = array ('success' => false, 'msg' => 'В файле не установлен #<list>' );
		elseif ($p2 == false)
			$result = array ('success' => false, 'msg' => 'В файле не установлен #</list>' );
		else {
			$list = $this->mapper->fetchURLs ();
			$newcnt = "#<list>\n";
			foreach ( $list as $l ) {
				$u = ($l['URL']{0}=='/') ? substr($l['URL'],1) : $l['URL'];
				$u = str_replace('(', '\(', $u);
				$u = str_replace(')', '\)', $u);
				$u = str_replace('+', '\+', $u);
				$p = strpos($u,'#',0);
				if ($p!==false)
					$u = substr($u,0,$p);
				
				//$newcnt .= 'RewriteRule ^' . $u . '/$ index.php?Id=' . $l ['Id'] . " [NC,L]\n";
				$newcnt .= 'RewriteRule ^' . $u . '$ index.php?Id=' . $l ['Id'] . " [NC,L]\n";
			}
			$newcnt .= "#</list>";
			$newcnt = substr ( $contents, 0, $p1 ) . $newcnt;
			$newcnt .= substr ( $contents, $p2 + 8 );
			fseek ( $f, 0 );
			fwrite ( $f, $newcnt );
			ftruncate ( $f, strlen ( $newcnt ) );
			$result = array ('success' => true, 'msg' => 'Список URL обновлен успешно' );
		}
		fclose ( $f );
		return $result;
	}*/
}

?>